package com.example.activity;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

import com.example.consts.Consts;
import com.example.dianping.R;
import com.example.entity.ResponseObject;
import com.example.entity.User;
import com.example.utils.MyUtils;
import com.example.utils.ShareUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;

public class MyLoginActivity extends Activity implements OnClickListener,
		PlatformActionListener {
	@ViewInject(R.id.username)
	EditText et_username;

	@ViewInject(R.id.password)
	EditText et_password;

	@ViewInject(R.id.et_vertify)
	EditText et_vertify;

	@ViewInject(R.id.tv_vertify)
	TextView tv_vertify;

	@ViewInject(R.id.tv_login_back)
	TextView tv_login_back;

	@ViewInject(R.id.reginter)
	TextView reginter;

	@ViewInject(R.id.login_btn)
	Button login_btn;

	@ViewInject(R.id.textView4)
	TextView tv_weibo;

	@ViewInject(R.id.textView6)
	TextView tv_qq;

	@ViewInject(R.id.textView5)
	TextView tv_weixin;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		ViewUtils.inject(this);
		ShareSDK.initSDK(this);
		et_vertify.setOnClickListener(this);
		login_btn.setOnClickListener(this);
		reginter.setOnClickListener(this);
		tv_weibo.setOnClickListener(this);
		tv_weixin.setOnClickListener(this);
		tv_qq.setOnClickListener(this);

		setRandomView(tv_vertify);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ShareSDK.stopSDK();
	}

	private void setRandomView(TextView tv_vertify) {
		tv_vertify.setText(MyUtils.getRandom(4));
	}

	public void loginQQ() {
		Platform platform = ShareSDK.getPlatform(this, QQ.NAME);
		platform.setPlatformActionListener(this);
		if(platform.isValid()){
			String uname = platform.getDb().getUserName();
			System.out.println("valid is pass");
			LoginSuccess(uname);
		}else{
			platform.showUser(null);
		}
	}

	public void loginByWeiXin() {
		Platform platform = ShareSDK.getPlatform(this, Wechat.NAME);
		platform.setPlatformActionListener(this);
		if(platform.isValid()){
			String uname = platform.getDb().getUserName();
			System.out.println("valid is pass");
			LoginSuccess(uname);
		}else{
			platform.showUser(null);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.et_vertify:
			setRandomView(tv_vertify);
			break;
		case R.id.textView4:
			loginWeiBo();
			break;
		case R.id.textView5:
			setRandomView(tv_vertify);
			loginQQ();
			break;
		case R.id.textView6:
			loginByWeiXin();
			break;

		case R.id.login_btn:
			handleLogin();
			break;
			
		case R.id.reginter:
			Intent intent = new Intent(this, MyRegisterActivity.class);
			startActivityForResult(intent, MyUtils.RequestRegisterCode);
		default:
			break;
		}
	}

	private void handleLogin() {
		final String username = et_username.getText().toString();
		final String password = et_password.getText().toString();
		System.out.println(Consts.USER_LOGIN + "&username="
				+ username + "&password=" + password);
		new HttpUtils().send(HttpMethod.GET, Consts.USER_LOGIN + "&username="
				+ username + "&password=" + password,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
//						ToastMaker.showToastInUiThread(MyLoginActivity.this,
//								"登录失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Gson gson = new GsonBuilder().create();
						ResponseObject<User> object = gson.fromJson(
								arg0.result,
								new TypeToken<ResponseObject<User>>() {
								}.getType());
						if (object.getState() == 1) {
							ShareUtils.putUserName(MyLoginActivity.this,
									username);
							LoginSuccess(username);
						}
						// ToastMaker.showToastInUiThread(MyLoginActivity.this,
						// "登录成功");
					}
				});

	}

	private void LoginSuccess(String username) {
//		String name = et_username.getText().toString();
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("username", username);
		setResult(MyUtils.RequestLoginCode, intent);
		finish();
	}

	private void loginWeiBo() {

	}

	@Override
	public void onCancel(Platform arg0, int arg1) {
		System.out.println("cancel the shouquan");
	}

	@Override
	public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
		String uname = arg0.getDb().getUserName();
		System.out.println("uname=="+uname);
		LoginSuccess(uname);
	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		System.out.println("shouquan is error");
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == MyUtils.RequestRegisterCode
				&& resultCode == MyUtils.RequestRegisterCode) {
			et_username.setText(data.getStringExtra("login_name"));
		}
	}

}
